Blockonomics Bitcoin payment gateway is the only in the e-commerce industry to allow full decentralization. Purchases made on your website are sent to your account directly, rather than to a wallet for payment gateways. Not only does this save you fees when taking out your coins, but it also allows you to take ownership of your revenue without the need for a middle man. Never lose income from wallet hacks payment gateway-be your own bank!
Why Blockonomics bitcoin payment gateway?
The fastest and easiest way to start accepting Blockonomics Bitcoin payments on your online eCommerce website. Blockonomics has helped thousands of eCommerce sites increase sales since 2015, by including Bitcoin, Ethereum and Litecoin as their customers ‘ payment option.
In this article, I will explain to you how to integrate the Blockonomics payment gateway to accept bitcoin payment using PHP.
Step-1: Before starting first, create your merchant account with https://www.blockonomics.co/
Step-2: Create a Database Configuration file (dbconfig.php)
2 3 4 5 6 7 8 9 10 11 12 |
$host=DB_HOST;// localhost $user= DB_USERNAME; $pass= DB_PASSWORD; $db= DB_NAME; $con = mysqli_connect($host,$user,$pass,$db); if(!$con) { die("Database connection error!"); } |
Step-3: Create a database table to store transaction details
copy and paste the following MySQL database query script to create a ‘transaction_details’ table.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
CREATE TABLE `transaction_details` ( `trans_id` int(255) NOT NULL, `order_id` int(255) NOT NULL, `tnx_id` longtext, `address` longtext NOT NULL, `value` varchar(1000) NOT NULL, `bits` varchar(1000) NOT NULL, `status` varchar(1000) NOT NULL, `created_on` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ) ENGINE=MyISAM DEFAULT CHARSET=latin1; ALTER TABLE `transaction_details` ADD PRIMARY KEY (`trans_id`); ALTER TABLE `transaction_details` MODIFY `trans_id` int(255) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1; |
In the above,
tnx_id = Transaction id,
address = Bitcoin payment address,
value= Total order amount in USD,
bits= Total order amount in bitcoin,
status = payment status (-1= pending, 2= success)
Step-4: Create your callback page (callback.php)
Copy and paste below code to update transaction status after payment.
2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php include 'dbconfig.php'; $txid = $_GET['txid']; $value = $_GET['value']; $status = $_GET['status']; $addr = $_GET['addr']; $trans_insert=mysqli_query($con, "UPDATE `transaction_details` set `tnx_id`='".$txid."', `value`='".$value."', `status`='".$status."' where `address`='".$addr."' "); ?> |
Step-5: Create Blockonomics Configurations file (blockConfig.php)
Copy and paste below Blockonomics Configurations code in blockConfig.php file
2 3 4 5 6 7 8 9 10 11 |
/*Blockonomics Configurations*/ $CALLBACK_SECRET = secret_key; // add your own secret key string, it will be same as you will use with your call back url later $BASE_URL = 'https://www.blockonomics.co'; $NEW_ADDRESS_URL = $BASE_URL.'/api/new_address?match_callback='.$CALLBACK_SECRET; $PRICE_URL = $BASE_URL.'/api/price?currency=USD'; $API_KEY = YOUR_API_KEY; |
Step-6: How to get API_KEY and set Callback URL
Callback URL should be like
2 3 4 5 6 |
https://www.tutorialswebsite.com/callback.php?secret=CALLBACK_SECRET // CALLBACK_SECRET string should be same as you defined in blockConfig.php file |
Step-7: Create a payment page (payment.php)
When your e-commerce website will process checkout then you need to store cart items details and customer details into the database. After inserting the order details, you need to get generated order id and pass generated order id and total order amount to payment.php file.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
<?php include 'dbconfig.php'; include 'blockConfig.php'; $order_id = 1234; $total_order_amount= 10; //(USD) //return here if cart is empty if(!isset($order_id) && empty($order_id)) { header("Location:error.php"); } $data = ''; $options = array( 'http' => array( 'header' => 'Authorization: Bearer '.$API_KEY, 'method' => 'POST', 'content' => $data ) ); //Generate new address for this invoice $context = stream_context_create($options); $contents = file_get_contents($NEW_ADDRESS_URL, false, $context); $new_address = json_decode($contents); //Getting price $options = array( 'http' => array( 'method' => 'GET') ); $context = stream_context_create($options); $contents = file_get_contents($PRICE_URL, false, $context); $price = json_decode($contents); if(isset($total_order_amount) && !empty($total_order_amount)){ $total_cost = $total_order_amount; }else{ header("Location:error.php"); } //Total Cart value in bits $bits = intval(1.0e8*$total_cost/$price->price); $status=-1; $tnx_query="select * from `transaction_details` where `order_id`='".$order_id."'"; $tnx_orderitem=mysqli_query($con,$tnx_query ); $getTnx=mysqli_fetch_array($tnx_orderitem); if(count($getTnx)==0){ $trans_insert=mysqli_query($con, "insert into `transaction_details` set `order_id`='".$order_id."',`tnx_id`='', `address`='".$new_address->address."',`value`='".$total_cost."', `bits`='".$bits."',`status`='".$status."'"); }else{ $trans_insert=mysqli_query($con, "UPDATE `transaction_details` set `address`='".$new_address->address."', `status`='".$status."' where `order_id`='".$order_id."' "); } ?> <section> <div class="box-heading"><h1>To pay, send exact amount of BTC to the given address</h1></div> <p>Amount</p> <p><strong><?php echo ($bits/1.0e8); ?></strong> BTC ⇌ <strong><?php echo $total_cost;?></strong> USD</p> <br/> <p>Bitcoin Address <span style="color:#f00;" id="qrstatus"></span></p> <input type="text" class="large-field numbers" value="<?php echo $new_address->address;?>" readonly="readonly"> </div> </div> <br> <p>QR Code</p> <img src="https://chart.googleapis.com/chart?chs=300x300&cht=qr&chl=<?php echo $new_address->address;?>&choe=UTF-8" /> <br/> <br/> </section> <script> jQuery(document).ready(function(){ setInterval(function() { $.ajax({ type:"POST", url: "ajax_payment_success.php", dataType : 'json', data:{order_id:<?php echo $order_id;?>}, success:function(data) { var resData=JSON.parse(JSON.stringify(data)); if(resData.status==2){ var url = "thankyou.php?order_id=<?php echo $order_id;?>"; window.location.href=url; } if(resData.status==-3){ jQuery('#qrstatus').text("(Payment Expired)") } if(resData.status==-2){ jQuery('#qrstatus').text("(Payment Error)") } if(resData.status==0){ jQuery('#qrstatus').text("(Payment Unconfirmed)") } if(resData.status==1){ jQuery('#qrstatus').text("(Payment Partially Confirmed)") } var current_addr="<?php echo $new_address->address;?>"; if(resData.status==-1 && current_addr !=resData.address){ location.reload(); } } }); }, 1000); }); </script> |
In the above code, you can see I used ajax to check payment status. After success payment, I am redirecting the customer to thankyou.php page.
Step-8: Create a page (ajax_payment_success.php )
Copy and paste below code to check payment status after every second interval
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php include 'dbconfig.php)'; if(!empty($_POST["order_id"])) { $order_id=$_POST["order_id"]; // $order_id=7; $query ="SELECT * FROM `transaction_details` WHERE `order_id`='".$order_id."'"; $res_prod=mysqli_query($con,$query); $product=mysqli_fetch_array($res_prod); echo json_encode($product); } ?> |
Step-9: Create a page (thankyou.php)
After successful payment redirect the customer to thank you page where you can show order details
2 3 4 5 |
<p>Thank you. Your order has been received.</p> <p>Order Number: <b><?php echo $_GET['order_id'];?></b></p> |
Step-10: Create a page (error.php)
2 3 4 |
<p>Error. OOPs Someting went wrong!</p> |
ALSO READ: Authorize.Net Payment Gateway Integration using PHP
ALSO READ: Integrate Recurring Stripe Subscription Payment with PHP
Conclusion:
This is the simple and easy steps to Integrate the Blockonomics bitcoin payment gateway in PHP. You can modify and customize code as per your requirement.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit a paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co